接下來要來分享在RecyclerView上使用ObjectAnimator的效果,使用ObjectAnimator能夠很輕易的做出一些簡單的動畫效果,像是翻轉、平移、放大縮小等等,在之後會使用翻轉的功能做出類似翻牌的效果,今天會來使用ObjectAnimator中的一些效果。
下面是在recyclerView中將內部元件水平翻轉的功能,下面的ObjectAnimator會先將動畫效果設定好,在第二個參數便是翻轉的屬性而後面的數值便是旋轉角度,在需要處發動畫時先設置動畫時間之後使用start播放,這樣子便能夠很神奇的做到翻牌的效果。
 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView myTextView;
        ImageView mImageView;
        ImageView cardView;
        ObjectAnimator animator = ObjectAnimator.ofFloat(itemView, "rotationY", 0, 180);
        ObjectAnimator animatorBack = ObjectAnimator.ofFloat(itemView, "rotationY", 180, 360);
        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.info_text);
            mImageView = itemView.findViewById(R.id.photo_item);
            cardView = itemView.findViewById(R.id.photo_stroke);
            //mImageView.setRotation(90);
            itemView.setOnClickListener(this);
        }.....
animator.setDuration(750);
animator.start();
animatorBack.setDuration(750);
animatorBack.start();
下面將列出其他ObjectAnimator的屬性
//平面旋轉
ObjectAnimator animator = ObjectAnimator.ofFloat(itemView,"rotation",0,360,0);
//平移
ObjectAnimator animator = ObjectAnimator.ofFloat(itemView, "translationX", 0, 100, -100,0); 
//縮放
ObjectAnimator animator = ObjectAnimator.ofFloat(itemView, "scaleX", 1, 2, 1); 
以上便是ObjectAnimator的基本使用。